<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport('#preserveWork', '设备保养信息查询')"
          v-hasPermi="['device:preserveWork:export']"
          >导出
        </el-button>
      </el-col>

      <!-- 分页效果 -->
      <el-col :span="2.2" style="position: absolute; right: 0px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      highlight-current-row
      border
      stripe
      v-loading="loading"
      id="preserveWork"
      :data="preserveWorkList"
      :height="600"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column
        label="#"
        fixed="left"
        type="index"
        width="40"
        align="center"
      />
      <el-table-column
        label="保养单号"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bylDjbh"
      />
      <el-table-column
        label="设备类型"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bylType"
      >
        <template v-slot="scope">
          <dict-tag :options="deviceTypeList" :value="scope.row.bylType" />
        </template>
      </el-table-column>
      <el-table-column
        label="设备编号"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bylCode"
      />
      <el-table-column
        label="设备名称"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bylName"
      />
      <el-table-column
        label="合模次数"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bylHmcs"
      />
      <el-table-column
        label="计划单号"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bylBpdh"
      />
      <el-table-column
        label="保养类别"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bylBylb"
      >
        <template v-slot="scope">
          <dict-tag :options="upKeepList" :value="scope.row.bylBylb" />
        </template>
      </el-table-column>
      <el-table-column
        label="保养人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bylByry"
      />
      <el-table-column
        label="开始时间"
        :show-overflow-tooltip="true"
        align="center"
        prop="bylKssj"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.bylKssj, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结束时间"
        :show-overflow-tooltip="true"
        align="center"
        prop="bylJssj"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.bylJssj, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="用时"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bylGhlj"
      />
      <el-table-column
        label="状态"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bylZtbz"
      >
        <template v-slot="scope">
          <span v-if="scope.row.bylZtbz == 1">进行中</span>
          <span v-if="scope.row.bylZtbz == 2">已完成</span>
        </template>
      </el-table-column>
      <el-table-column
        label="开单人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bylJlry"
      />
      <el-table-column
        label="开单日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="bylJlrq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.bylJlrq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="结案人员"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="bylJary"
      />
      <el-table-column
        label="结案日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="bylJarq"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.bylJarq, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        fixed="right"
        align="center"
        width="100"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleProcessFiles(scope.row)"
            :disabled="scope.row.docPath == null"
            v-hasPermi="['device:preserveWork:query']"
            >附件
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="recordClick(scope.row)"
            :disabled="scope.row.bylZtbz !== 2"
            v-hasPermi="['device:preserveWork:query']"
            >保全记录
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="400px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="开单日期">
            <el-date-picker
              v-model="dateRangeBylJlrq"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="保养单号" prop="bylDjbh">
            <el-input
              v-model="queryParams.bylDjbh"
              placeholder="请输入保养单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="设备编号" prop="bylCode">
            <el-input
              v-model="queryParams.bylCode"
              placeholder="请输入设备编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="设备类型" prop="bylType">
            <el-select
              v-model="queryParams.bylTypes"
              multiple
              placeholder="请选择设备类型"
              clearable
              size="small"
            >
              <el-option
                v-for="item in deviceTypeList"
                :key="item.dictValue"
                :label="'[' + item.dictValue + ']' + item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 保全记录对话框 -->
    <el-dialog
      :class="$style.tabSbby"
      :title="title"
      v-model="openFix"
      :close-on-click-modal="false"
      draggable
      width="830px"
      append-to-body
    >
      <el-form
        :model="formFix"
        :rules="rules"
        ref="formSbby"
        style="display: flex; flex-wrap: wrap"
        label-width="80px"
      >
        <el-form-item label="保养单号" prop="bymDjbh">
          <el-input
            disabled
            v-model="formFix.bymDjbh"
            style="width: 180px"
            size="mini"
          />
        </el-form-item>
        <el-form-item label="保养类别" prop="bymBylb">
          <el-select
            v-model="formFix.bymBylb"
            disabled
            size="mini"
            style="width: 180px"
            filterable
            placeholder="请选择保养类型"
          >
            <el-option
              v-for="dict in upKeepList"
              :key="dict.dictValue"
              :label="'[' + dict.dictValue + ']' + dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="计划单号" prop="bymBpdh">
          <el-input
            disabled
            v-model="formFix.bymBpdh"
            style="width: 180px"
            size="mini"
          />
        </el-form-item>

        <el-form-item label="产品型号" prop="bymCpxh">
          <el-input
            disabled
            v-model="formFix.bymCpxh"
            style="width: 180px"
            size="mini"
          />
        </el-form-item>

        <el-form-item label="工装编号" prop="bymGzbh">
          <el-input
            disabled
            v-model="formFix.bymGzbh"
            style="width: 180px"
            size="mini"
          />
        </el-form-item>

        <el-form-item label="使用设备" prop="bymSysb">
          <el-input
            disabled
            v-model="formFix.bymSysb"
            style="width: 180px"
            size="mini"
          />
        </el-form-item>

        <el-form-item label="模具编号" prop="bymMjbh">
          <el-input
            disabled
            v-model="formFix.bymMjbh"
            style="width: 180px"
            size="mini"
          />
        </el-form-item>

        <el-form-item label="模具名称" prop="mjmMjmc">
          <el-input
            disabled
            v-model="formFix.mjmMjmc"
            style="width: 180px"
            size="mini"
          />
        </el-form-item>

        <el-form-item label="模具尺寸" prop="mjmMjgg">
          <el-input
            disabled
            v-model="formFix.mjmMjgg"
            style="width: 180px"
            size="mini"
          />
        </el-form-item>

        <el-form-item label="制作时间" prop="mjmDcrq">
          <el-input
            disabled
            v-model="formFix.mjmDcrq"
            style="width: 180px"
            size="mini"
          />
        </el-form-item>

        <el-form-item label="腔型数量" prop="mjmXsms">
          <el-input
            disabled
            v-model="formFix.mjmXsms"
            style="width: 180px"
            size="mini"
          />
        </el-form-item>
        <el-form-item label="样件描述" prop="bymYjms">
          <el-input
            disabled
            v-model="formFix.bymYjms"
            style="width: 180px"
            size="mini"
          />
        </el-form-item>
      </el-form>
      <el-tabs v-model="activeName">
        <el-tab-pane label="保全工作" name="first">
          <hd-table
            :columns="[
              { title: '#', type: 'index', width: '40' },
              { title: '保全类', key: 'bnlNrlb', width: '100' },
              { title: '保全项目', key: 'bnlNrms', width: '348' },
              { title: '保养内容', key: 'bydBynr', width: '100' },
              { title: '保全人员', key: 'bydZrry', width: '100' },
              { title: '备件情况', key: 'bydDesc', width: '100' },
            ]"
            v-model="maintenanceWorkList"
            :readOnly="true"
          >
            <template #bydBynr="scope">
              <el-input disabled v-model="scope.row.bydBynr" />
            </template>
            <template #bydZrry="scope">
              <el-input disabled v-model="scope.row.bydZrry" />
            </template>
            <template #bydDesc="scope">
              <el-input disabled v-model="scope.row.bydDesc" />
            </template>
          </hd-table>
        </el-tab-pane>
        <el-tab-pane label="恢复精度工作" name="second">
          <hd-table
            :columns="sys_m_child_3"
            v-model="recoveryAccuracyList"
            :readOnly="true"
          />
        </el-tab-pane>
        <el-tab-pane label="其他保全检查项目" name="third">
          <hd-table
            :columns="[
              { title: '#', type: 'index', width: '40' },
              { title: '检查项目', key: 'bydJcxmms' },
              {
                title: '是否检查',
                key: 'bydChkjg',
                width: '120',
                align: 'center',
              },
            ]"
            v-model="otherProjectsList"
            :readOnly="true"
          >
            <template #bydChkjg="scope"
              ><el-checkbox disabled v-model="scope.row.bydChkjg"
            /></template>
          </hd-table>
        </el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="openFix = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPreserveWork,
  listPreserveContext,
  listPreserveInfo,
  listPreserveWork,
} from "@/api/device/preserveWork";
import { getEffectiveDeviceType } from "@/api/device/deviceType";
import { exportTable } from "@/utils/ruoyi";
import HdTable from "@/components/HdTable";

export default {
  name: "PreserveWorkSelect",
  components: { HdTable },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 默认选择第一个表
      activeName: "first",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      totalAdd: 0,
      // 设备保养作业表格数据
      preserveWorkList: [],
      // 所有设备表
      deviceAllList: [],
      // 设备类型表
      deviceTypeList: [],
      rowData: [],
      selectionChildData: [],
      // 保全工作
      maintenanceWorkList: [],
      // 恢复精度工作
      recoveryAccuracyList: [],
      // 其他保全项目检查
      otherProjectsList: [],
      // 保养类别
      upKeepList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openFix: false,
      // 开单日期时间范围
      dateRangeBylJlrq: [
        new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 30 * 3),
        new Date(new Date().getTime() + 1000 * 60 * 60 * 24),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 25 : 17,
        bylDjbh: null,
        bylCode: null,
        bylBpdh: null,
        bylZtbz: 2,
        bylJlrq: null,
        bylTypes: null,
        params: null,
      },
      queryParamsChild: {
        pageNum: 1,
        pageSize: 25,
        bylType: "A",
        params: {
          find: "",
        },
      },
      // 表单参数
      form: {},
      formFix: {},
      // 表单校验
      rules: {
        bylBylb: [
          { required: true, message: "保养类别不能为空", trigger: "blur" },
        ],
      },
      sys_m_child_3: [
        {
          title: "#",
          type: "index",
          width: 40,
          align: "center",
          fixed: "left",
        },
        {
          type: "selection",
          width: 40,
        },
        {
          title: "设备问题",
          key: "bydWtms",
          minWidth: 120,
          sortable: true,
        },
        {
          title: "分析产生原因",
          key: "bydYyms",
          minWidth: 150,
          sortable: true,
        },
        {
          title: "解决措施",
          key: "bydGsms",
          sortable: true,
        },
        {
          title: "维修人员",
          key: "bydZrry",
          minWidth: 120,
          sortable: true,
        },
        {
          title: "备件情况",
          key: "bydDesc",
          width: 120,
          sortable: true,
        },
      ],
    };
  },
  created() {
    getEffectiveDeviceType().then((res) => {
      this.deviceTypeList = res.data;
      for (let item of this.deviceTypeList) {
        item.dictLabel = item.dtypeName;
        item.listClass = "default";
        item.dictValue = item.dtypeCode;
      }
    });
    this.getAllDicts("device_upkeep_style").then((response) => {
      this.upKeepList = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询设备保养作业列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.dateRangeBylJlrq && "" != this.dateRangeBylJlrq) {
        this.queryParams.params["beginBylJlrq"] = this.moment(
          this.dateRangeBylJlrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endBylJlrq"] = this.moment(
          this.dateRangeBylJlrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      listPreserveWork(this.queryParams).then((response) => {
        this.preserveWorkList = [];
        this.preserveWorkList = this.preserveWorkList.concat(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bylId: null,
        bylDjbh: null,
        bylType: [],
        bylCode: null,
        bylName: null,
        bylHmcs: null,
        bylBpdh: null,
        bylBylb: null,
        bylBynr: null,
        bylByry: null,
        bylKssj: null,
        bylJssj: null,
        bylGhlj: null,
        bylZtbz: 1,
        bylJlrq: null,
        bylJlry: null,
        bylJary: null,
        bylJarq: null,
        params: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.preserveWorkList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeBylJlrq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.bylId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.rowData = selection;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bylId = row.bylId || this.ids;
      getPreserveWork(bylId).then((response) => {
        this.form = response.data;
        this.form.bylType = this.form.bylType.split(",");
        this.open = true;
        this.title = "修改设备保养作业";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询设备保养作业";
      this.openSearch = true;
    },
    /** 导出按钮操作 */
    handleExport(id, title) {
      exportTable(id, title);
    },
    /** 分页 */
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 附件点击事件 */
    handleProcessFiles(row) {
      let files = process.env.VUE_APP_BASE_API + row.docPath;
      window.open(files);
    },
    /** 保养记录点击事件 */
    recordClick(row) {
      const id = row.bylDjbh;
      listPreserveInfo(id).then((res) => {
        this.formFix = res.data;
      });
      listPreserveContext(id, "1").then((res) => {
        this.maintenanceWorkList = res.data;
      });
      listPreserveContext(id, "2").then((res) => {
        this.recoveryAccuracyList = res.data;
      });
      listPreserveContext(id, "3").then((res) => {
        this.otherProjectsList = res.data;
      });
      this.openFix = true;
    },
  },
};
</script>

<style module lang="scss">
.hdDivButton {
  button {
    width: calc(50% - 5px);
  }
}

.tabSbby {
  :global {
    .el-form-item {
      margin-bottom: 5px;
    }
    .el-table .cell {
      height: 28px;
      line-height: 28px;
    }
    .el-input--medium .el-input__inner {
      height: 28px;
      line-height: 28px;
      padding: 0px;
    }
  }
}
</style>
